<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 100px;
            height: 100px;
        }

        .container1 {
            height: 200px;
        }

        .container2 {
            height: 200px;
        }

        .container3 {
            height: 200px;
        }

        .container1 .box1 {
            background: red;
            display: block;
        }

        .container1 .box2 {
            background: blue;
            display: none;
        }

        .container1 .box3 {
            background: yellow;
            display: none;
        }

        .container2 .box1 {
            background: red;
            display: block;
        }

        .container2 .box2 {
            background: blue;
            display: none;
        }

        .container2 .box3 {
            background: yellow;
            display: none;
        }

        .container3 .box1 {
            background: red;
            display: block;
        }

        .container3 .box2 {
            background: blue;
            display: none;
        }

        .container3 .box3 {
            background: yellow;
            display: none;
        }
    </style>
</head>

<body>
    <div class="container1">
        <button>按钮一</button>
        <button>按钮二</button>
        <button>按钮三</button>
        <div class="box1"></div>
        <div class="box2"></div>
        <div class="box3"></div>
    </div>

    <div class="container2">
        <button>按钮一</button>
        <button>按钮二</button>
        <button>按钮三</button>
        <div class="box1"></div>
        <div class="box2"></div>
        <div class="box3"></div>
    </div>

    <div class="container3">
        <button>按钮一</button>
        <button>按钮二</button>
        <button>按钮三</button>
        <div class="box1"></div>
        <div class="box2"></div>
        <div class="box3"></div>
    </div>
    <script>
        //多个选项卡,提高复用性,函数封装
        let btns = document.querySelectorAll(".container1 button");
        let divs = document.querySelectorAll(".container1 div");

        let btn2s =document.querySelectorAll(".container2 button"); 
        let div2s = document.querySelectorAll(".container2 div");   

        let btn3s =document.querySelectorAll(".container3 button");
        let div3s = document.querySelectorAll(".container3 div"); 

        //封装函数
        tab=(btns,divs)=>btns.forEach((item,key)=>item.onclick=()=>divs.forEach((div,k)=>key===k?div.style.display="block":div.style.display="none"))
        tab(btns,divs)
        tab(btn2s,div2s)
        tab(btn3s,div3s)
    </script>
</body>

</html>